package com.jeegod.ribbon.command;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

public class HelloCommand extends HystrixCommand<List<String>> {

    private List<Long> ids;

    private RestTemplate restTemplate;

    public HelloCommand(String commandGroupKey, RestTemplate restTemplate, List<Long> ids){
        // 根据commandGroupKey进行线程隔离
        super(HystrixCommandGroupKey.Factory.asKey(commandGroupKey));
        this.restTemplate = restTemplate;
        this.ids = ids;
    }

    @Override
    protected List<String> run() throws Exception {
        System.out.println("发送请求。。。参数为：" + ids.toString() + Thread.currentThread().getName());
        String[] result = restTemplate.getForEntity("http://HELLO-SERVICE1/hjcs?ids={1}", String[].class, StringUtils.join(ids, ",") ).getBody();

        return Arrays.asList(result);
    }
}
